package com.java.al.swing;

import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

import twaver.network.TNetwork;
import twaver.network.toolbar.BaseNetworkButton;

public class ToolbarDemo extends JPanel {

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setContentPane(new ToolbarDemo());
		frame.setSize(500, 400);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}

	private TNetwork network = new TNetwork();

	public ToolbarDemo() {
		initGUI();
	}

	JPopupMenu menu = createMenu();

	private void initGUI() {
		final BaseNetworkButton button = new BaseNetworkButton(network);
		button.setText("Menu");
		network.getToolbar().addSeparator();
		network.getToolbar().add(button);

		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Point point = button.getLocation();
				menu.show(network.getToolbar(), point.x, point.y + button.getHeight());
			}
		});
		this.setLayout(new BorderLayout());
		this.add(network, BorderLayout.CENTER);
	}

	private JPopupMenu createMenu() {
		JPopupMenu popMenu = new JPopupMenu();
		JMenuItem item = new JMenuItem("item1");
		popMenu.add(item);
		item = new JMenuItem("item2");
		popMenu.add(item);
		JMenu menu = new JMenu("submenu");
		for (int i = 0; i < 3; i++) {
			item = new JMenuItem("sub" + i);
			menu.add(item);
		}
		popMenu.add(menu);
		return popMenu;
	}
}
